package com.tanhua.server.controller;

import com.baomidou.mybatisplus.extension.api.R;
import com.tanhua.model.vo.PageResult;
import com.tanhua.server.service.CommentsService;
import com.tanhua.server.service.SmallVideosService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

@RestController
@RequestMapping("/smallVideos")
public class SmallVideosController {

    @Autowired
    private SmallVideosService videosService;

    @Autowired
    private CommentsService commentsService;


    /**
     * 发布视频
     *  接口路径：POST
     *  请求参数：
     *      videoThumbnail：封面图
     *      videoFile：视频文件
     */
    @PostMapping
    public ResponseEntity saveVideos(MultipartFile videoThumbnail, MultipartFile videoFile) throws IOException {
        videosService.saveVideos(videoThumbnail,videoFile);
        return ResponseEntity.ok(null);
    }

    /**
     * 视频列表
     */
    @GetMapping
    public ResponseEntity queryVideoList(@RequestParam(defaultValue = "1")  Integer page,
                                         @RequestParam(defaultValue = "10") Integer pagesize) {
        PageResult result = videosService.queryVideoList(page, pagesize);
        return ResponseEntity.ok(result);
    }
    /**
     * 小视频 发布评论
     * */
    @PostMapping("/{id}/comments")
    public ResponseEntity publishComments(@PathVariable("id") String videoId,@RequestBody Map map){
        String comment = (String) map.get("comment");
        commentsService.saveVideoComments(videoId,comment);
        return ResponseEntity.ok(null);
    }

    /**
     * 小视频 评论点赞
     * */
    @PostMapping("/comments/{id}/like")
    public ResponseEntity likeComment(@PathVariable("id") String commentId){
        videosService.likeComment(commentId);
        return ResponseEntity.ok(null);
    }

    /**
     * 小视频 取消评论点赞
     * */
    @PostMapping("/comments/{id}/dislike")
    public ResponseEntity dislikeComment(@PathVariable("id") String commentId){
        videosService.dislikeComment(commentId);
        return ResponseEntity.ok(null);
    }
    /**
     * 分页查询视频评论
     * */
    @GetMapping("/{id}/comments")
    public ResponseEntity findComments(@RequestParam(defaultValue = "1") Integer page,
                                       @RequestParam(defaultValue = "10") Integer pagesize,
                                       @PathVariable("id") String movementId) {
        PageResult pr = commentsService.findComments(movementId,page,pagesize);
        return ResponseEntity.ok(pr);
    }

    /**
     * 视频点赞
     */
    @PostMapping("/{id}/like")
    public ResponseEntity like(@PathVariable("id") String videoId) {
        commentsService.likeVideoComment(videoId);
        return ResponseEntity.ok(null);
    }

    /**
     * 视频取消点赞
     */
    @PostMapping("/{id}/dislike")
    public ResponseEntity dislike(@PathVariable("id") String videoId) {
        commentsService.dislikeVideoComment(videoId);
        return ResponseEntity.ok(null);
    }

    /**
     * 视频用户关注
     */
    @PostMapping("{uid}/userFocus")
    public ResponseEntity userFllow(@PathVariable("uid") String lookId){
        videosService.userFllow(Long.valueOf(lookId));
        return ResponseEntity.ok(null);
    }

    /**
     * 视频用户关注 - 取消
     */
    @PostMapping("{uid}/userUnFocus")
    public ResponseEntity userUnFocus(@PathVariable("uid") String lookId){
        videosService.userUnFocus(Long.valueOf(lookId));
        return ResponseEntity.ok(null);
    }
}